//
// Created by gogoqiu on 2021/8/20.
//

#ifndef UNTITLED2_PREFERENCE_H
#define UNTITLED2_PREFERENCE_H
#include <wx/wx.h>
#include <utility>
#include <vector>
#include <sqlite3.h>
#include "PreferenceItem.h"
//#include "MyTagApp.h"

using namespace std;

/*
 * PreferenceItem
 * pair, key, value
 * wxString key, int value
 */
class PreferenceView;


struct OnPreferenceStorageSession{
    void OnRunSqlOk();
    void OnRunSqlError(wxString errMsg);
};

/**
 *
 */
class PreferenceStorage {
protected:
  vector<PreferenceItem*> preferences;
  //default
  //vector<PreferenceItem*> _preferences;
  //root view, list view

public:
  ~PreferenceStorage(){
      vector<PreferenceItem*>::iterator _iterator;
      for(_iterator = preferences.begin();_iterator!=preferences.end(); _iterator++ ){
          //preferences.swap()
          delete _iterator.operator*();
          _iterator.operator*() = 0;
      }
      //vector<Preference> tmp;
      //preferences.swap( tmp );
  }

  void add( PreferenceItem *preference ){
      preferences.push_back( preference );
  }

  //void BuildView();

  virtual int Save() = 0;

  virtual int Load() = 0;

  virtual int InitTable() = 0;

  virtual int updateItem( const wxString& key, const wxString&  ) = 0;

  vector<PreferenceItem*> getPreferences() {
    return preferences;
  }

  virtual int RunSql( wxString sql ) = 0;
};

//
class MysqlPreferenceStorage: public PreferenceStorage{
public:
    //init
    MysqlPreferenceStorage();
    ~MysqlPreferenceStorage();
    //virtual int Save();
    int Save() override;
};

class MyTagApp;

class SqlitePreferenceStorage: public PreferenceStorage{
    //
    sqlite3* m_pDb;
    wxString tag;

    //static SqlitePreferenceStorage* instance;
protected:

    MyTagApp * myTagApp;
public:
    //
    //static SqlitePreferenceStorage* getDefaultSharedPreferences( const wxString& TAG );
    //
    int RunSql( wxString sql )override;

    explicit SqlitePreferenceStorage( MyTagApp * myTagApp );

    ~SqlitePreferenceStorage();

    int Save() override;
    int Save( vector<PreferenceItem*> items );

    int Load() override;

    int InitTable() override;

    int updateItem( const wxString& key, const wxString&  ) override;
};

/*
 * PreferenceStorage
 */
/*
class PreferenceScreen{
public:
    virtual void BuildView( wxDialog *) =0;
};*/

#ifdef __Win32__
class RegisterPreferenceStorage: public PreferenceStorage{

};
#endif
#endif //UNTITLED2_PREFERENCE_H
